﻿using NFine.Code;
using NFine.Data;
using NFine.Domain.Entity.BusinessManage;
using NFine.Domain.Entity.SystemManage;
using NFine.Domain.IRepository.BusinessManage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace NFine.Repository.BusinessManage
{
    public class D_IndentRepository : RepositoryBase<D_IndentEntity>, ID_IndentRepository
    {
        public void CheckForm(D_IndentEntity indentEntity, string keyValue)
        {
            using (var db = new RepositoryBase())
            {
                D_IndentEntity ientity = db.FindEntity<D_IndentEntity>(keyValue);
                if (ientity.F_EnabledMark == true)
                    throw new Exception("该订单已发货！");
                ientity.F_LastModifyUserId = indentEntity.F_LastModifyUserId;
                ientity.F_LastModifyTime = indentEntity.F_LastModifyTime;
                ientity.F_Company = indentEntity.F_Company;
                ientity.F_FastNo = indentEntity.F_FastNo;
                ientity.F_EnabledMark = true;
                db.Update(ientity);
            }
        }

        public List<D_IndentEntity> GetIndentGridJson(Expression<Func<D_IndentEntity, bool>> predicate, Pagination pagination)
        {
            using (var db = new RepositoryBase())
            {
                var baselinq = from sh in db.DbSet<D_IndentEntity>().Where(predicate)
                               join u1 in db.DbSet<UserEntity>() on sh.D_M_F_ID equals u1.F_Id
                               orderby sh.F_CreatorTime descending
                               select new { sh, u1 };
                //baselinq.
                //var basel = baselinq.Provider.CreateQuery(predicate);
                pagination.records = baselinq.Count();

                var query = baselinq.Skip(pagination.rows * (pagination.page - 1)).Take(pagination.rows).AsQueryable();

                List<D_IndentEntity> lst = new List<D_IndentEntity>();
                foreach (var obj in query)
                {
                    obj.sh.F_MemAccount = obj.u1.F_Account;
                    obj.sh.F_MemName = obj.u1.F_RealName;
                    lst.Add(obj.sh);
                }
                return lst;
            }
        }

        public List<D_IndentEntity> FindMenList(Expression<Func<D_IndentEntity, bool>> predicate, Expression<Func<UserEntity, bool>> predicate1, Pagination pagination)
        {
            using (var db = new RepositoryBase())
            {
                var baselinq = from sh in db.DbSet<D_IndentEntity>().Where(predicate)
                               join u1 in db.DbSet<UserEntity>().Where(predicate1) on sh.D_M_F_ID equals u1.F_Id
                               orderby sh.F_CreatorTime descending
                               select new { sh, u1 };
                //baselinq.
                //var basel = baselinq.Provider.CreateQuery(predicate);
                pagination.records = baselinq.Count();

                var query = baselinq.Skip(pagination.rows * (pagination.page - 1)).Take(pagination.rows).AsQueryable();

                List<D_IndentEntity> lst = new List<D_IndentEntity>();
                foreach (var obj in query)
                {
                    obj.sh.F_MemAccount = obj.u1.F_Account;
                    obj.sh.F_MemName = obj.u1.F_RealName;
                    lst.Add(obj.sh);
                }
                return lst;
            }
        }

        public D_IndentEntity GetMemberIndentFormJson(string keyValue)
        {
            using (var db = new RepositoryBase())
            {
                var baselinq = from sh in db.DbSet<D_IndentEntity>()
                               join u1 in db.DbSet<UserEntity>() on sh.D_M_F_ID equals u1.F_Id
                               where sh.F_Id.Equals(keyValue)
                               orderby sh.F_CreatorTime descending
                               select new { sh, u1 };
                //baselinq.
                //var basel = baselinq.Provider.CreateQuery(predicate);
               
                D_IndentEntity lst = new D_IndentEntity();
                foreach (var obj in baselinq)
                {
                    obj.sh.F_MemAccount = obj.u1.F_Account;
                    obj.sh.F_MemName = obj.u1.F_RealName;
                    lst=obj.sh;
                }
                return lst;
            }
        }
    }
}
